package com.example.bigdata.scala.模式匹配

object matchDemo {
    /**
      * 模式匹配允许我们在多个条件之间进行编程选择。
      *     带下划线_的最后一种情况是通配符。它匹配任何未定义在上面的情况下。
      */
    def main(args: Array[String]): Unit = {

        printNum(0)
        printNum(1)
        printNum(2)
        println(fib(5))
    }

    def printNum(int:Int): Unit ={
        int match {
            case 0 => println("Zero")
            case 1 => println("One")
            case _ => println("more than one")
        }
    }

    def objIf(): Unit ={
        val a = 7
        val obj = if(a==1) 1
        else if (a==2) "2"
        else if (a==3) BigInt(3)
        else if (a==4) Map("aa" -> 1)
        else if (a==5) Map(1->"aa")
        else if (a==6) Array(1,2,3)
        else if (a==7) Array("aa",1)
        else if (a==8) Array("aa")
        println("obj = "+ obj)

        val result = obj match {
            case  a:Int => a
            case  b:Map[String,Int] => "对象是一个字符串 - 数字的map集合"
            case  c:Map[Int,String] => "对象是一个数字-字符串的map集合"
            case  d:Array[String] => "对象是一个字符串数组"
            case  e:Array[Int]=> "对象是一个数字数组"
            case  f:BigInt => Int.MaxValue
            case  _ => "啥也不是"

        }
        println("result : " + result)

    }
    def fib(in:Int) :Int = in match {
        case 0 => 0
        case 1 => 1
        case n => fib(n - 1) + fib(n - 2)
    }
    // fib(n - 1) + fib(n - 2)    fib(4)+fib(3) fib(4) = 3 + 2 = 5   fib(3) = 2 + 0


}
